package com.qingding.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qingding.R;

/**
 * Created by Administrator on 2018-01-08.
 */
//这是一个产生对话框的类
public class MAlertDialog extends Dialog {
    public MAlertDialog(@NonNull Context context) {
        super(context);
    }

    public MAlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }
    /**
     * Helper class for creating a custom dialog
     */
    //这是一个非常重要的静态内部类，用于构造该对话框
    public static class Builder {

        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;//积极按钮的文字
        private String negativeButtonText;//消极按钮的文字
        private View contentView;

        //定义DialogInterface接口中的OnClickListener内部接口的成员变量
        private DialogInterface.OnClickListener
                positiveButtonClickListener,
                negativeButtonClickListener;

        //构造方法
        public Builder(Context context) {
            this.context = context;
        }

        //我发现这里面的每一个方法都会返回一个Builder对象，也就是说可以一鼓作气设置对话框的各种属性
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        //设置事件监听器，为positiveButtonText赋值
        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener){
//            this.positiveButtonText = (String) context
//                    .getText(positiveButtonText);
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
        //其实我有点不明白同一个方法为什么要写两遍
        /*public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }*/

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
//            this.negativeButtonText = (String) context
//                    .getText(negativeButtonText);
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        //这里原先是有两个这种方法，现在我将它们都注释掉了，如果有问题再回来改
        /*public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }*/

        //将前面的所有属性值全都设置好了之后，我们就可以俩建造这个对话框，该方法返回一个该类的实例（MAlertDialog）
        public MAlertDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            //创建MAlertDialog对象
            final MAlertDialog dialog = new MAlertDialog(context,
                    R.style.Dialog);
            //实例化一个对话框的组件
            View layout = inflater.inflate(R.layout.alert_dialog, null);
            dialog.addContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    (layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                            dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ( layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                            dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(
                        R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView,
                                new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.WRAP_CONTENT,
                                        LinearLayout.LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
